<?php
namespace Think\Upload\Driver;
    /**
     *
     * @authors Your Name (you@example.org)
     * @date    2014-07-11 13:42:25
     * @version $Id$
     */

//require_once dirname(__FILE__) . '/Qcloud/cos-autoloader.php';
require(__DIR__ . DIRECTORY_SEPARATOR . '/Qcloud/cos-autoloader.php');

class Qclouds
{
    /**
     * 保存指定文件
     * @param  array $file 保存的文件信息
     * @param  boolean $replace 同名文件是否覆盖
     * @return boolean          保存状态，true-成功，false-失败
     */
    public function save($imgDir, $filename)
    {
        $content = fopen("./" . $imgDir . $filename, 'r');
        $client = $this->client();
//        $client = new \Qcloud\Cos\Client(array('region' =>C('COS_REGION'),
//            'credentials' => array(
//                'appId' => C('COS_APPID'),
//                'secretId' => C('COS_KEY'),
//                'secretKey' => C('COS_SECRET'))));

        #putObject
        try {
            $result = $client->putObject(array(
                'Bucket' => 'jz' . '-' . C('COS_APPID'),
                'Key' => $filename,
                'Body' => $content,
                'ServerSideEncryption' => 'AES256'));
            return array('code' => 1, 'result' => $result->getData());
        } catch (\Exception $e) {
            return array('code' => -1, 'result' => "$e\n");
        }
    }

    //创建client对象
    function client()
    {
        $client = new \Qcloud\Cos\Client(array('region' =>C('COS_REGION'),
            'credentials' => array(
                'appId' => C('COS_APPID'),
                'secretId' => C('COS_KEY'),
                'secretKey' => C('COS_SECRET'))));
        return $client;
    }
}